package com.ply.flashsalessystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ply.flashsalessystem.annotation.AuthorityName;
import com.ply.flashsalessystem.annotation.UserAuthority;
import com.ply.flashsalessystem.entity.pojo.ShopCar;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.entity.vo.user.ShopCarForGoodsVo;
import com.ply.flashsalessystem.entity.wrapper.ShopCarWrapper;
import com.ply.flashsalessystem.service.ShopCarService;
import com.ply.flashsalessystem.service.UserService;
import com.ply.flashsalessystem.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/user")
@Api("购物车 接口")
@CrossOrigin
public class ShopCarController {
    @Autowired
    UserService userService;

    @Autowired
    ShopCarService shopCarService;


//    @AuthorityName(authorityId = 38, authorityName = "用户 加入购物车")
    @UserAuthority
    @ApiOperation("加入购物车")
    @PostMapping("addShopCar/{goodsId}/{goodsNums}")
    public Result addShopCar(@PathVariable("goodsId") Integer goodsId,
                             @PathVariable("goodsNums") double goodsNums) {

        if (goodsId <= 0 || goodsNums <= 0) {
            return Result.error().message("参数错误");
        }
        return userService.addShopCar(goodsId, goodsNums)
                ? Result.ok().message("加入购物车成功")
                : Result.error().message("加入购物车失败");
    }

//    @AuthorityName(authorityId = 39, authorityName = "用户 删除购物车")
    @UserAuthority
    @ApiOperation("删除购物车")
    @DeleteMapping("deleteForShopCar/{shopCarId}")
    public Result deleteForShopCar(@PathVariable("shopCarId") Integer shopCarId) {
        return userService.deleteGoodsInShopCar(shopCarId)
                ? Result.ok().message("删除购物车成功")
                : Result.error().message("删除购物车失败");
    }

//    @AuthorityName(authorityId = 40, authorityName = "用户 查看购物车中的所有信息")
    @UserAuthority
    @ApiOperation("查看购物车中的所有信息")
    @PostMapping(value = {"getGoodsOfShopCar/{current}/{limit}", "getGoodsOfShopCar"})
    public Result getGoodsOfShopCar(@PathVariable(value = "current", required = false) Integer current,
                                    @PathVariable(value = "limit", required = false) Integer limit,
                                    @RequestBody(required = false) ShopCarWrapper shopCarWrapper) {
        if (shopCarWrapper == null) {
            shopCarWrapper = new ShopCarWrapper();
        }
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }
        Page<ShopCarForGoodsVo> goodsOfShopCar = userService.getGoodsOfShopCar(current, limit, shopCarWrapper);
        return Result.ok().data("total", goodsOfShopCar.getTotal()).data("rows", goodsOfShopCar.getRecords());
    }


    //修改购物车 数量
//    @AuthorityName(authorityId = 41, authorityName = "用户 修改购物车数量")
    @UserAuthority
    @ApiOperation("修改购物车 数量")
    @PostMapping("updateShopCar/{shopCarId}/{newNums}")
    public Result updateShopCar(@PathVariable("shopCarId") Integer shopCarId,
                                @PathVariable("newNums") double newNums) {
        ShopCar byId = shopCarService.getById(shopCarId);
        if (byId == null) {
            return Result.error().message("参数错误");
        }
        if (!Objects.equals(byId.getUserId(), UserUtils.getUserId())) {
            return Result.error().message("不是该用户的购物车 不允许 修改");
        }
        if (newNums <= 0) {
            return Result.error().message("新的数量错误");
        }
        byId.setGoodsNum(newNums);
        shopCarService.updateById(byId);

        return Result.ok().message("修改成功");
    }
}

